﻿
$.extend({
    format: function () {
        if (arguments.length == 0)
            return null;
        var str = arguments[0];
        for (var i = 1; i < arguments.length; i++) {
            var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
            str = str.replace(re, !arguments[i] ? "" : arguments[i]);
        }
        return str;
    },
    no2ChineseNo: function (no) {
        var rst = "";
        var arrn = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
        var arrp = ['', '十'];
        var arr = no.toString().split('').reverse();
        if (arr.length == 1 || arr.length == 2) {
            for (var i = arr.length - 1; i >= 0; i--) {
                if ((i == 1 && arr[i] == 1))
                    rst += arrp[i];
                else if (i == 0 && arr[i] == 0)
                    continue;
                else
                    rst += arrn[arr[i]] + arrp[i];
            }
            return rst;
        }
        return no;
    },
    LockScreen: function () {

        var div = "<div id='_LockScreenDIV' style='position: absolute;z-index: 99999;'><img src='/_imgs/Loading.gif'></div>";
        $("body").append($(div))
        var Ptop = ($("body").height()) / 2;
        var Pleft = ($("body").width()) / 2;
        $("#_LockScreenDIV").offset({ top: Ptop, left: Pleft });


        // div = "<div id='_LockScreenDIV' style='left: 0;top: 0;width: 100%;height: 100%;position: absolute;text-align: center;background: transparent;overflow: hidden;z-index: 99999;'><img src='/Images/Loading.gif'></div>";


    },
    unLockScreen: function () {
        $("#_LockScreenDIV").remove()
    },
    toast: function (msg) {

        var div = "<div id='_ToastDIV' class='_ToastDIV' style='text-align:center;position: fixed;z-index: 99999;background-color:#09c;padding:20px;max-width:800px;min-width:400px'><span style='font-size: 18px; font-family: \"Microsoft YaHei\";color:#fff'>" + msg + "</span></div>";
        $("body").append($(div))
        //var Ptop = ($("body").height() - $("#_ToastDIV").height()) / 2;
        var Ptop = 200;
        var Pleft = ($("body").width() - $("#_ToastDIV").width()) / 2;
        //$("#_ToastDIV").offset({ top: Ptop, left: Pleft });
        $("._ToastDIV").css({ top: Ptop, left: Pleft });
        setTimeout('$("#_ToastDIV").remove()', 1000);
    },
    getQueryString: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(decodeURIComponent(r[2])); return null;
    },
    //获取cookie
    getCookie: function () {
        var re = new Object();
        var co = document.cookie.split(";")
        for (var i = 0; i < co.length; i++) {
            var _name = co[i].split("=")[0];
            var _value = co[i].split("=")[1];
            re[_name.trim()] = _value;
        }
        return re;
    },
    postOpen: function (url, data, name) {
        var tempForm = $("<form></form>");
        tempForm.attr("id", "_tempForm");
        tempForm.attr("method", "post");
        tempForm.attr("action", url);
        tempForm.attr("target", name);
        var hideInput = $("<input></input>");
        hideInput.attr("type", "hidden");
        hideInput.attr("name", "_content");
        hideInput.val(data);
        tempForm.append(hideInput);
        $("body").append(tempForm);
        $(tempForm).on("onsubmit", function () { window.open(url) });
        tempForm.submit();
        $("body").remove(tempForm);
    },
    handleError: function (s, xhr, status, e) {
        // If a local callback was specified, fire it  
        if (s.error) {
            s.error.call(s.context || s, xhr, status, e);
        }

        // Fire the global callback  
        if (s.global) {
            (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]);
        }
    }
});

//接管AJAX
var __ajax = $.ajax;
$.ajax = function (p) {
    var __error = p.error;
    p.error = function (XMLHttpRequest, textStatus, errorThrown) {
        if (XMLHttpRequest.responseText.indexOf("90909CF3-AB38-4609-8A83-11B4BB23FE23BF612FC4-DE2A-4B97-AA73-03B1EE34B36B") > 0) {
            window.location.href = '/mPages/login.aspx?web=&url=' + window.location.href;
        }
        else {
            p.error = __error(XMLHttpRequest, textStatus, errorThrown);
        }
    }
    __ajax(p);
    //重载alert
}


if (typeof (Common) == 'undefined') {
    Common = {};
}










Common.AjaxError = function (xhr, textStatus, errorThrow) {
    $.TL_unLockScreen();
    if (xhr.getResponseHeader("Non-Auth") != null) {
        location.href = "/Web/Login/Login.aspx";
    }
    else if (xhr.getResponseHeader("Custom-Ex") != null) {

        Common.alert(decodeURI(xhr.getResponseHeader("Custom-Ex")), xhr.responseText);
    }
    else {
        Common.alert("ajax错误", "");
    }
}

//自定义alert
Common.alert = function (message, detail) {

    var templateifame = '<div id="ajax-alert" class="alert alert-danger alert-dismissible" role="alert">\
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">\
                                <span aria-hidden="true">&times;</span>\
                            </button>\
                            <strong>错误!</strong>\
                            <p id="message">{0}</p>\
                            <iframe id="detail" frameborder="0" name="detail" style="display:none;"></iframe>\
                            <a id="showDetail" href="javascript:void(0)"  class="btn btn-link">查看详情</a>\
                        </div>';


    if ($("#ajax-alert").length == 0) {
        var html = $.format(templateifame, message);
        var $html = $(html);
        $html.css({ position: "fixed", margin: "0 auto", left: "0px", right: "0px", top: "50px", width: "400px" });// $html.css({ position: "fixed", left: (document.body.clientWidth - $html.outerWidth()) / 2 + "px", top: "50px" });

        $("body").append($html);
        window.frames["detail"].document.write(detail);
        $("#showDetail").click(function () {
            $("#detail").show();
        });

    }
    else {
        $("#ajax-alert #message").html(message);
        window.frames["detail"].document.write("");
        window.frames["detail"].document.close();
        window.frames["detail"].document.write(detail);
    }

}